package com.uchengwang.ui.cm_ui.uu.http;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.config.UCityConfig;
import com.uchengwang.functions.http.config.UUConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.cm_ui.uu.bean.ClubInviteBean;
import com.uchengwang.ui.cm_ui.uu.bean.ClubListInviteBean;
import com.uchengwang.ui.cm_ui.uu.bean.CmtmsgBean;
import com.uchengwang.ui.cm_ui.uu.bean.CmtmsgListBean;
import com.uchengwang.ui.cm_ui.uu.bean.NoticeBean;
import com.uchengwang.ui.cm_ui.uu.bean.SystemNotifyBean;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/3.
 */

public class UUHttpManager {

    private static UUHttpManager manager;
    private UUHttpManager(){}
    private AlertDialog dialog;

    public static UUHttpManager getManager() {
        if (manager == null){
            manager = new UUHttpManager();
        }
        return manager;
    }


    /**
     * 撤回/取消撤回一条享说
     * @param id 享说的ID
     *  @param activity 当前享说的状态
     * */
    public void recallSpeek(final Activity activity, final long id, final boolean isActivi, final HttpCallBack callBack){
        dialog = DialogUtils.getUtils().showDialogSingle(activity, "确定撤回?", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.getUtils().cancel();

                LoadingUtils.getUtils().loading(activity);
                MapRequest map = new MapRequest();
                map.put(BaseConfig.ID, id);
                map.put(BaseConfig.IS_ACTIVI, !isActivi);
                HttpManager.newInstance(activity).query(UUConfig.RecallSpeek.method, UUConfig.RecallSpeek.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        LoadingUtils.getUtils().hide();
                        Log.d("jjf", "status="+status+"=msg="+msg);
                        if (callBack != null){
                            callBack.onResp(status, msg, null);
                        }
                        UCommonUtil.getUtil().showToast(activity, "请重试");
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        LoadingUtils.getUtils().hide();

                        Log.d("jjf", "status="+status+"=msg="+msg);

                        if (status != 0){
                            UCommonUtil.getUtil().showToast(activity, msg);
                            return;
                        }
                        if (callBack != null){
                            callBack.onResp(status, msg, null);
                        }else {
                            BaseConfig.isRelease = true;
                            activity.finish();
                        }
                    }
                });

            }
        });

    }

    /**
     * 删除一条享说
     * */
    public void deleteSpeek(final Activity activity, final long shareId, final HttpCallBack callBack){
        dialog = DialogUtils.getUtils().showDialogSingle(activity, "确定删除这条享说?", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.getUtils().cancel();

                LoadingUtils.getUtils().loading(activity);
                MapRequest map = new MapRequest();
                map.put(BaseConfig.SHARE_ID, shareId);
                HttpManager.newInstance(activity).query(UUConfig.Speek.method_delete, UUConfig.Speek.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        LoadingUtils.getUtils().hide();
                        Log.d("jjf", "status="+status+"=msg="+msg);
                        if (callBack != null){
                            callBack.onResp(status, msg, null);
                        }
                        UCommonUtil.getUtil().showToast(activity, "请重试");
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        LoadingUtils.getUtils().hide();

                        Log.d("jjf", "status="+status+"=msg="+msg);

                        if (status != 0){
                            UCommonUtil.getUtil().showToast(activity, msg);
                            return;
                        }
                        if (callBack != null){
                            callBack.onResp(status, msg, null);
                        }else {
                            BaseConfig.isRelease = true;
                            activity.finish();
                        }
                    }
                });
            }
        });
    }

    /**
     *获取社团邀请 邀请加入社团
     */
    public void getClubInvites(Activity activity, int page, int perPage, final ClubInviteListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 10);
        HttpManager.newInstance(activity).query(ClubConfig.InviteRecord.method, ClubConfig.InviteRecord.URL_USER, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                if (status != 0 || TextUtils.isEmpty(data) || data.equals("[]")){
                    listener.onResult(status, 0, null);
                    return;
                }

                ClubListInviteBean bean = JsonUtils.getManager().stringToBean(data, ClubListInviteBean.class);
                List<ClubInviteBean> listBean = bean.getData();
                listener.onResult(status, bean.getTotalPage(), listBean);

            }
        });
    }

    /**
     * 社团获取自己的邀请记录
     * */
    public void getInvites(Activity activity, int page, int perPage, final ClubInviteListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 10);
        HttpManager.newInstance(activity).query(ClubConfig.InviteRecord.method, ClubConfig.InviteRecord.URL_ORG, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                if (status != 0 || TextUtils.isEmpty(data) || data.equals("[]")){
                    listener.onResult(status, 0, null);
                    return;
                }

                ClubListInviteBean bean = JsonUtils.getManager().stringToBean(data, ClubListInviteBean.class);
                List<ClubInviteBean> listBean = bean.getData();
                listener.onResult(status, bean.getTotalPage(), listBean);

            }
        });
    }



    /**
     * 获取别人对你发布的信息进行评论的评论信息
     * @param isCheck 是否刷新数据的已读状态,false=后台不会更新状态
     * */
    public void getCmtMsg(Activity activity, int page, int perPage, boolean isCheck, final CmtMsgListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        map.put(BaseConfig.IS_CHECK, isCheck);
//        if (isRead != null){
////            map.put(BaseConfig.IS_READ, isRead);
//        }
        HttpManager.newInstance(activity).query(UCityConfig.CmtUnRead.method, UCityConfig.CmtUnRead.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);

                if (status == 0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    CmtmsgListBean bean = JsonUtils.getManager().stringToBean(data, CmtmsgListBean.class);
                    List<CmtmsgBean> list = bean.getData();

                    listener.onResult(status, bean.getTotalPage(), list);
                }else {
                    listener.onResult(status, 0, null);
                }

            }
        });
    }

    /**
     * 获取反馈
     * */
    public void getFeedbacks(Activity activity, final OnResultListener listener){
        HttpManager.newInstance(activity).query(UserConfig.Feedback.method, UserConfig.Feedback.URL, null, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0,null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, data);
            }
        });
    }
    /**
     * 获取反馈未读消息数
     * */
    public void getFeedbackNums(Activity activity, final OnResultListener listener){
        HttpManager.newInstance(activity).query(UserConfig.Feedback.method, UserConfig.Feedback.URL_NUM, null, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                if (status ==0 && !TextUtils.isEmpty(data)){
                    JSONObject jsonObject = JsonUtils.getManager().getJSONObject(data);
                    int unRead = jsonObject.getInteger("unread");
                    listener.onResult(status, msg, unRead, null);
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }


    /**
     * 获取系统通知
     * */
    public void getSystemNotifys(final Activity activity, final SystemListener listener){
        final List<SystemNotifyBean> addList = new ArrayList<>();
//        if (!UCommonUtil.getUtil().isUni()){
            //获取反馈[获取的是用户的反馈]
            HttpUserManager.getManager().getMyNotice(activity, 1, 1, false, false, new OnHttpResultListener<NoticeBean>() {
                @Override
                public void onResult(int status, String msg, int total, List<NoticeBean> list) {
                    SystemNotifyBean systemNotifyBean = null;
                    if (total >0){
                        systemNotifyBean = new SystemNotifyBean(R.mipmap.uchenglogo, "Uni团队", "你有一条新的反馈信息", 0, true);
                    }else {
                        systemNotifyBean = new SystemNotifyBean(R.mipmap.uchenglogo, "Uni团队", "暂无反馈信息", 0, false);
                    }
                    addList.add(systemNotifyBean);

                    listener.onResult(addList);

//                    getSystemNotifys(activity, addList, listener);
                }
            });

//            getFeedbackNums(activity, new OnResultListener() {
//                @Override
//                public void onResult(int stutas, String msg, int total, String data) {
//                    SystemNotifyBean systemNotifyBean = null;
//                    if (total >0){
//                        systemNotifyBean = new SystemNotifyBean(R.drawable.notify_uni, "系统通知", "你有一条新的反馈信息", 0, true);
//                    }else {
//                        systemNotifyBean = new SystemNotifyBean(R.drawable.notify_uni, "系统通知", "暂无反馈信息", 0, false);
//                    }
//                    addList.add(systemNotifyBean);
//
//                    getSystemNotifys(activity, addList, listener);
//                }
//            });
//        }else {
//            HttpUniManager.getManager().getFeedback(activity, 1, 1, new HttpUniManager.UniFeedbackListener() {
//                @Override
//                public void onResult(int stutas, String msg, int totalPage, List<UniFeedbackBean> list) {
//                    SystemNotifyBean bean = null;
//                    if (list != null && list.size() >0){
//                        UniFeedbackBean b = list.get(0);
//                        if (!b.isRead()){
//                            bean = new SystemNotifyBean(R.drawable.notify_uni, "系统通知", "你有一条新的反馈信息", 0, true);
//                        }else {
//                            bean = new SystemNotifyBean(R.drawable.notify_uni, "系统通知", "暂无反馈信息", 0, false);
//                        }
//                    }else {
//                        bean = new SystemNotifyBean(R.drawable.notify_uni, "系统通知", "暂无反馈信息", 0, false);
//                    }
//
//                    addList.add(bean);
//
//                    getSystemNotifys(activity, addList, listener);
//                }
//            });
//        }
    }

    /**
     * 获取系统通知2
     * */
    public void getSystemNotifys(final Activity activity, final List<SystemNotifyBean> addList, final SystemListener listener){
        //获取评论
        getCmtMsg(activity, 1, 1,false,  new CmtMsgListener() {
            @Override
            public void onResult(int stutas, int totalPages, List<CmtmsgBean> listBean) {
                SystemNotifyBean notifyBean = null;
                if (stutas ==0 && listBean != null && listBean.size() >0){
                    CmtmsgBean bean = listBean.get(0);
                    notifyBean = new SystemNotifyBean(R.drawable.notify_join, "参与号", "评论信息: "+bean.getUnreadMessage(), bean.getCreateTime(), !bean.isRead());
                }else {
                    notifyBean = new SystemNotifyBean(R.drawable.notify_join, "参与号", "暂无评论信息", 0, false);
                }
                addList.add(notifyBean);
                //再查询群邀请申请未读数
                RyGroupManager.getManager().getMyUnread(activity, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        SystemNotifyBean systemNotifyBean;
                        if (total >0){
                            systemNotifyBean= new SystemNotifyBean(R.drawable.notify_group, "群助手", "新的群信息", System.currentTimeMillis(), true);

                        }else {
                            systemNotifyBean = new SystemNotifyBean(R.drawable.notify_group, "群助手", "暂无群信息", System.currentTimeMillis(), false);
                        }
                        addList.add(systemNotifyBean);


                        switch (UChengApplication.userRole){
                            case BaseConfig.STUDENT_ROLE:
                                //获取社团邀请
                                getClubInvites(activity, 1, 1, new ClubInviteListener() {
                                    @Override
                                    public void onResult(int stutas, int totalPages, List<ClubInviteBean> listBean) {
                                        if (listBean != null && listBean.size() >0){
                                            ClubInviteBean bean = listBean.get(0);
                                            SystemNotifyBean systemNotifyBean = new SystemNotifyBean(R.drawable.notify_club, "社团号", bean.getOrgName()+" 邀请您加入", bean.getCreateTime(), !bean.isRead());
                                            addList.add(systemNotifyBean);
                                        }else {
                                            SystemNotifyBean systemNotifyBean = new SystemNotifyBean(R.drawable.notify_club, "社团号", "暂无社团邀请", 0, false);
                                            addList.add(systemNotifyBean);
                                        }
                                        //将最后的结果返回
                                        listener.onResult(addList);
                                    }
                                });
                                break;
                            case BaseConfig.CLUB_ROLE:
                                //获取社团邀请
                                getInvites(activity, 1, 1, new ClubInviteListener() {
                                    @Override
                                    public void onResult(int stutas, int totalPages, List<ClubInviteBean> listBean) {
                                        if (listBean != null && listBean.size() >0){
                                            ClubInviteBean bean = listBean.get(0);
                                            SystemNotifyBean systemNotifyBean = new SystemNotifyBean(R.drawable.notify_club, "社团号", "您邀请 "+bean.getInvitedNickName()+" 加入社团", bean.getCreateTime(), !bean.isRead());
                                            addList.add(systemNotifyBean);
                                        }else {
                                            SystemNotifyBean systemNotifyBean = new SystemNotifyBean(R.drawable.notify_club, "社团号", "暂无社团邀请", 0, false);
                                            addList.add(systemNotifyBean);
                                        }
                                        //将最后的结果返回
                                        listener.onResult(addList);
                                    }
                                });
                                break;
                            case BaseConfig.UNI_ROLE:
                            case BaseConfig.EDITOR_ROLE:
                            case BaseConfig.ZONG_ROLE:
                                listener.onResult(addList);
                                break;
                        }

                    }
                });
            }
        });
    }


    public interface SystemListener{
        void onResult(List<SystemNotifyBean> list);
    }



    public interface ClubInviteListener{
        void onResult(int stutas, int totalPages, List<ClubInviteBean> listBean);
    }

    public interface CmtMsgListener{
        void onResult(int stutas, int totalPages, List<CmtmsgBean> listBean);
    }



}
